﻿var missingPassword = "Inserisci la password";
var differentPassword = "Le password inserite non coincidono";
var differentEmail = "Le email inserite non coincidono";
var failedLogin = "Login errato, ritenta";
var successfulRegistration = "Complimenti, registrazione effettuata con successo!";

// check if a variable is defined

function isdefined(variable) {
    return eval('(typeof(' + variable + ') != "undefined");');
}

//funzione eseguita in caso di utente autenticato

function userAuthenticated(id) {
    var html = "<div class=\"risposta\">";
    html += "<div class=\"testo\">" + id + "</div>";
    html += "<img class=\"foto\" src=\"images/" + id + ".png\" />";
    html += "<a href=\"#\" class=\"testo\">Rank</a>";
    html += "<a href=\"?user=" + id + "\" class=\"testo\">Profilo</a>";
    html += "<a href=\"?logout=" + id + "\" id=\"logout\" class=\"testo\">Logout</a></div>";
    return html;
}

// funzione che controlla il successo o meno del login

function checkLogin(value) {
    try {
        var obj = jQuery.parseJSON(value);
        if (obj.user_id != null) {
            var user = obj.user_id;
            if (obj.risposta) return user;
        }
    }
    catch (error) {
        alert("Error! The server answered:\n " + error);
    }
}

var c = 0;
var cl;

function waitAjax() { 
    return "<img src=\"images/ajax-loader.gif\" style=\"margin-top:69px;\" alt=\"Ajax\" id=\"Loader\" />";
}

function getRank() {
    c++;
    $("#cont").html(waitAjax());
    setTimeout("readRank()",10000);
}

function readRank() {
    $("#Loader").hide();
    $.post("Rank.aspx", '{"event":"rank"}', function(resp) {
    try {
        //alert(resp);
            var obj = jQuery.parseJSON(resp);
            
            //cl = "<strong>Classifica</strong><br /><br />";
            cl = "";
            for (var i in obj) {
                if (i != "TagImage")
                    cl += i + " : " + obj[i] + "<br />";
            }
            $("#cont").html(cl);
            cl = "";
        } catch (error) { alert("classifica illeggibile: " + error); }
    });
}

//inizio codice jQuery
var regAttempts = 0;

$(document).ready(function() {
    
    setInterval("getRank()", 10000);

    //send login data to server by JSON
    $("#login_form").submit(function() {
        var pwd = $("#Login_password").val();
        if (pwd != "") {
            $.getScript("script/md5.js", function(resp_1) {
                var html;
                var richiesta = '{"event":"login","login":"' + $("#Login_name").val() + '", "password":"' + MD5(pwd) + '"}';
                $.post("Login.aspx", richiesta, function(response) {
                    var risp = checkLogin(response);
                    if (risp != null) {
                        html = userAuthenticated(risp);
                        $("#login_area").html(html);
                    }
                    else $("#failure").html("<span style=\"color:Red; font-weight:bold;\">" + failedLogin + "</span>");
                });
            });
        }
        else //alert(missingPassword);
            $("#failure").html("<span style=\"color:Red; font-weight:bold;\">" + missingPassword + "</span>");
        return false;
    });
});